package cn.stylefeng.guns.modular.mall.controller;

import cn.stylefeng.guns.core.common.constant.factory.PageFactory;
import cn.stylefeng.guns.core.common.page.PageInfoBT;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.modular.merchandise.service.IMerService;
import cn.stylefeng.guns.modular.order.service.IOrderService;
import cn.stylefeng.guns.modular.shoppingcart.service.IShoppingcartService;
import cn.stylefeng.guns.modular.shoppingcart.service.impl.ShoppingcartServiceImpl;
import cn.stylefeng.guns.modular.system.model.Mer;
import cn.stylefeng.guns.modular.system.model.Order;
import cn.stylefeng.guns.modular.system.model.Shoppingcart;
import cn.stylefeng.guns.modular.system.warpper.MerWrapper;
import cn.stylefeng.guns.modular.system.warpper.OrderWrapper;
import cn.stylefeng.guns.modular.system.warpper.ShoppingCartWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang3.StringUtils;
import org.beetl.ext.fn.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 商城主控制器
 *
 * @author baikun
 * @Date 2020-04-26 17:00:50
 */
@Controller
@RequestMapping("/mall")
public class MallController extends BaseController {
    @Autowired
    private IMerService merService;
    @Autowired
    private IShoppingcartService shoppingcartService;
    @Autowired
    private IOrderService orderService;

    private String PREFIX = "/mall/";


    /**
     * 跳转到商城首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "index.html";
    }

    @RequestMapping("/home")
    public String home() {
        return PREFIX + "home.html";
    }

    @RequestMapping("/commodity")
    public String commodity(Model model) {
        return PREFIX + "commodity.html";
    }

    @RequestMapping("/buytoday")
    public String buytoday() {
        return PREFIX + "buytoday.html";
    }

    @RequestMapping("/information")
    public String information() {
        return PREFIX + "information.html";
    }

    @RequestMapping("/about")
    public String about() {
        return PREFIX + "about.html";
    }

    @RequestMapping("/login")
    public String login() {
        return PREFIX + "login.html";
    }

    @RequestMapping("/shopcart")
    public String shopcart(Model model) {
        Integer userId = ShiroKit.getUser().getId();
        EntityWrapper<Shoppingcart> shoppingcartEntityWrapper = new EntityWrapper<Shoppingcart>();
        Wrapper<Shoppingcart> shoppingcart = shoppingcartEntityWrapper.eq("userid",userId);
        List<Map<String, Object>> shoppingcartList = shoppingcartService.selectMaps(shoppingcart);
        model.addAttribute("shoppingcartList",new ShoppingCartWrapper(shoppingcartList).wrap());
        return PREFIX + "shopcart.html";
    }

    @RequestMapping("/details/{id}")
    public String details(@PathVariable Integer id, Model model) {
        Mer mer = merService.selectById(id);
        model.addAttribute("mer",mer);
        return PREFIX + "details.html";
    }

    @RequestMapping("/order")
    public String order() {
        return PREFIX + "order.html";
    }

    @RequestMapping(value = "/detail_list")
    @ResponseBody
    public Object list(Integer category, String mername) {
        Page<Mer> page = new PageFactory<Mer>().defaultPage();
        if(ToolUtil.isNotEmpty(mername)){
            mername = StringUtils.deleteWhitespace(mername);
        }
        List<Map<String, Object>> result = merService.list(page,category,mername);
        page.setRecords(new MerWrapper(result).wrap());
        return new PageInfoBT<>(page);

    }

    @RequestMapping("/search")
    public String search(String mername, RedirectAttributes model){
        model.addFlashAttribute("mername",mername);
        return REDIRECT + "/mall/commodity";
    }







}
